package exer;

/**
 * @author tongchen
 * @create 2022-09-28 15:38
 */
public class LeecodeTesst {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        int[]arr={1,2,6,3,4,5,6};
        for (int i=0;i< arr.length;++i){
            linkedList.add(new Node(arr[i]));
        }
        linkedList.removeElements(linkedList.root,6);
    }
}
class LinkedList{
    Node root;
    public void add(Node node){

        if (root==null){
            root=node;
        }

        else {
            Node temp=root;
            while (temp.next!=null){
                temp=temp.next;
            }
            temp.next=node;
        }
    }
    public Node removeElements(Node head, int val) {
        if(head==null){
            return null;
        }

        //头结点单独处理
        while(true){
            if(head.val==val){
                head=head.next;
            }
            if(head.val!=val){
                break;
            }
            if(head.next==null){
                return null;
            }
        }
        //建立辅助结点
       Node temp=head;
        //处理其他节点
        while(temp.next!=null){
            if(temp.next.val==val&&temp.next.next!=null){
                temp.next=temp.next.next;
            }
            if(temp.next.val==val&&temp.next.next==null){
                temp.next=null;
            }
            temp=temp.next;
        }
        return head;
    }
}
class Node{
    int val ;
    Node next;

    public Node(int value) {
        this.val = value;
    }
}
